package com.aj.activity;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.aj.activity.adapter.PersonalAlbumAdapter;
import com.aj.databases.entry.LookPhotoInfo;
import com.aj.databases.entry.PhotoDateInfo;
import com.aj.databases.entry.PhotoInfo;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.AlbumPhotoQueryRequest;
import com.aj.http.request.PhotoCannelShareRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.LookZonePhotoResponse;
import com.aj.utils.Contants;

public class PersonalAlbumSharedActivity extends BaseActivity {
	private ListView listbody;

	private String zoneId;
	private String userId;
	private List<PhotoDateInfo> photoDateInfoList;
	private PersonalAlbumAdapter adapter;
	private static final String TAG = PersonalAlbumSharedActivity.class
			.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_person_album);
		initView();
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			List<PhotoInfo> photoList;
			switch (v.getId()) {
			case R.id.righttext_1:
				if (photoDateInfoList == null || photoDateInfoList.size() == 0) {
					Toast.makeText(PersonalAlbumSharedActivity.this,
							"没有选中任何图片~", Toast.LENGTH_LONG).show();
					return;
				}
				photoList = PersonalAlbumAdapter.choosePhotoList;
				if (photoList == null || photoList.size() == 0) {
					Toast.makeText(PersonalAlbumSharedActivity.this,
							getString(R.string.personalbum_nochoose_text),
							Toast.LENGTH_LONG).show();
					return;
				}
				String photoIds = "";
				for (PhotoInfo photoInfo : photoList) {
					photoIds += photoInfo.getPhotoId() + ",";
				}
				photoIds = photoIds.substring(0, photoIds.lastIndexOf(","));
				Log.e(TAG, "photoIds:" + photoIds);
				// 分享图片
				showProgressDialog();
				new PhotoCannelShareRequest().excute(
						PersonalAlbumSharedActivity.this, userId, zoneId,
						photoIds, "0", new IHttpCallBack() {
							@Override
							public void success(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									if (common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
										finish();
										if (SharePhotoActivity.instance != null) {
											SharePhotoActivity.instance
													.finish();
											SharePhotoActivity.instance = null;
										}
									} else {
										Toast.makeText(
												PersonalAlbumSharedActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void fail(String result) {
								showProgressDialog();
							}
						});
				break;
			default:
				break;
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		resume();
	}

	private void resume() {
		showProgressDialog();
		new AlbumPhotoQueryRequest().excute(PersonalAlbumSharedActivity.this,
				userId, getIntent().getStringExtra(Contants.ALBUM_ID_PARAMS),
				50, 0, new IHttpCallBack() {
					@Override
					public void success(String result) {
						try {
							dimissProgressDialog();
							LookZonePhotoResponse response = new LookZonePhotoResponse(
									new JSONObject(result));
							Common common = response.getCommon();
							if (common != null
									&& common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								LookPhotoInfo lookPhotoInfo = response
										.getLookPhotoInfo();
								photoDateInfoList = lookPhotoInfo
										.getPhotoDateInfoList();
								if (photoDateInfoList != null
										&& photoDateInfoList.size() > 0) {
									adapter = new PersonalAlbumAdapter(
											PersonalAlbumSharedActivity.this,
											photoDateInfoList);
									listbody.setAdapter(adapter);
									adapter.setManageFlag(true);
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String result) {
						dimissProgressDialog();
					}
				});
	}

	private void initView() {
		zoneId = getIntent().getStringExtra(Contants.ZONE_ID_PARAMS);
		userId = getIntent().getStringExtra(Contants.USER_ID_PARAMS);
		setActionBarTitle(getIntent().getStringExtra(Contants.TITLE_PARAMS));
		ClickEvent clickEvent = new ClickEvent();
		setRightTextAndClickEvent1("完成", clickEvent);
		listbody = (ListView) findViewById(R.id.listbody);
	}
}
